package com.ltkj.baseplatform.controller;

import com.ltkj.baseplatform.domain.ArtChannel;
import com.ltkj.baseplatform.domain.SimpleValue;
import com.ltkj.baseplatform.domain.bo.ArtChannelBo;
import com.ltkj.baseplatform.domain.bo.ArtChannelQueryBo;
import com.ltkj.baseplatform.service.IArtChannelService;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import com.ltkj.common.security.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章管理-栏目Controller
 *
 * @author liupf
 * @date 2023-08-28
 */
@RestController
@RequestMapping("/channel")
@Api(tags = "文章管理-栏目相关")
public class ArtChannelController extends BaseController {
    @Autowired
    private IArtChannelService artChannelService;

    /**
     * 查询文章管理-栏目列表
     */
    @ApiOperation("查询文章管理-栏目列表")
    @RequiresPermissions("baseplatform:channel:list")
    @GetMapping("/list")
    public AjaxResult list(ArtChannelQueryBo artChannelQueryBo) {
        List<ArtChannel> list = artChannelService.selectArtChannelList(artChannelQueryBo);
        return success(list);
    }

    /**
     * 查询栏目树
     */
    @RequiresPermissions("baseplatform:channel:list")
    @GetMapping(value = "/tree")
    @ApiOperation("查询栏目树")
    public AjaxResult channelTree() {
        return AjaxResult.success(artChannelService.selectChannelTreeList());
    }

    /**
     * 获取文章管理-栏目详细信息
     */
    @RequiresPermissions("baseplatform:channel:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("根据id查询栏目信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(artChannelService.selectArtChannelById(id));
    }

    /**
     * 新增文章管理-栏目
     */
    @RequiresPermissions("baseplatform:channel:add")
    @Log(title = "文章管理-栏目", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增栏目数据")
    public AjaxResult add(@Validated @RequestBody ArtChannelBo artChannelBo) {
        return toAjax(artChannelService.insertArtChannel(artChannelBo));
    }

    /**
     * 修改文章管理-栏目
     */
    @RequiresPermissions("baseplatform:channel:edit")
    @Log(title = "文章管理-栏目", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("编辑栏目数据")
    public AjaxResult edit(@Validated @RequestBody ArtChannelBo artChannelBo) {
        return toAjax(artChannelService.updateArtChannel(artChannelBo));
    }

    /**
     * 删除文章管理-栏目
     */
    @RequiresPermissions("baseplatform:channel:remove")
    @Log(title = "文章管理-栏目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除栏目数据")
    public AjaxResult remove(@PathVariable Long id) {
        if (artChannelService.hasChildByChannelId(id)) {
            return warn("存在下级栏目,不允许删除");
        }
        if (artChannelService.checkChannelExistContent(id)) {
            return warn("栏目下存在文章,不允许删除");
        }
        Long[] ids = {id};
        return toAjax(artChannelService.deleteArtChannelByIds(ids));
    }

    /**
     * 将栏目数据放入回收站
     */
    @RequiresPermissions("baseplatform:channel:edit")
    @Log(title = "文章管理-栏目", businessType = BusinessType.UPDATE)
    @PostMapping("/recycle")
    @ApiOperation("将栏目数据放入回收站")
    public AjaxResult recycle(@Validated @RequestBody SimpleValue.Ids ids) {
        return toAjax(artChannelService.recycle(ids.getIds(), artChannelService.selectChannelListByIds(ids.getIds())));
    }
}
